package edu.yourl.web;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import edu.yourl.application.YourlSession;
import edu.yourl.domain.exception.NoRegisteredUserException;
import edu.yourl.presenter.ISignInPresenter;

public class SignInPage extends WebPage implements ISignInPage {

	private String user;
	private String password;
	
	@SpringBean
	private ISignInPresenter presenter;
	
	public SignInPage() {
		
		add(new FeedbackPanel("feedbackPanel"));
		Form<?> signInForm = new Form<Object>("signInForm") {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit() {
				try {
					presenter.doSignIn(SignInPage.this);
					setResponsePage(HomePage.class);
				} catch (NoRegisteredUserException e) {
					error("Error en su usuario o password. Intentelo nuevamente");
				}
			}
		};
		TextField<String> user = new TextField<String>("user", new PropertyModel<String>(this,"user"));
		user.setRequired(true);
		PasswordTextField password = new PasswordTextField("password", new PropertyModel<String>(this,"password"));
		signInForm.add(user,password);
		add(signInForm);
		add(new Link<Void>("registrationLink"){
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(RegistrationPage.class);
			}
		});
	}
	
	
	public String getUser() {
		return user;
	}


	public String getPassword() {
		return password;
	}

	@Override
	public YourlSession getSession() {
		return (YourlSession)super.getSession();
	}
	
	public void setPresenter(ISignInPresenter presenter) {
		this.presenter = presenter;
	}
	
}
